package net.zjitc.arrays;

public class TriangularMatrix {
    private int[] arr;

    public TriangularMatrix() {
    }

    public TriangularMatrix(int[] arr) {
        this.arr = arr;
    }

    /**
     * 将下三角二维矩阵arr压缩 (1+n)*n/2
     * @param arr
     */
    public void compress(int[][] arr){
      int length = (arr.length+1)*arr.length/2;
       this.arr = new int[length];
        int k = 0;
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j <= i; j++) {
                 this.arr[k]= arr[i][j];
                 k++;

            }
        }
    }

    /**
     * 获得第i行第j列的元素的值
     * @param i
     * @param j
     * @return （i,j）-->值
     */
    public int getValue(int i,int j){
       if (j>i){
           return 0;
       }
       int index = (i+1)*i/2+j;
       return arr[index];
    }


}
